<?php

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';
ini_set('memory_limit', '512M');

use Biz\model\brandAccount\BrandAccountInfo;

class DataMigrate
{

    private $sourceRightsNameMap = [];

    private $sourceRightsTypeMap = [];

    /**
     * 构造权益Map
     * @return void
     */
    private function getSourceRightsKv()
    {
        $sourceRights = \Biz\model\brandRights\BrandRightsSource::selectList(['id[>]' => 0]);
        if ($sourceRights && is_array($sourceRights)) {
            $this->sourceRightsNameMap = array_column($sourceRights, 'id', 'rights_name');
            $this->sourceRightsTypeMap = array_column($sourceRights, 'rights_type', 'id');
        }
    }

    /**
     * 迁移记录
     * @param $message
     * @param $data
     * @return void
     */
    private function mlog($message, $data)
    {

        \Doraemon\tools\Log::info($message, $data);
    }

    /**
     * desc 生成套餐权益配置数据
     * @return false|void
     */
    public function createPackageRightsData()
    {
        $this->getSourceRightsKv();
        $list = \Biz\model\brandRights\Tmp::selectList([]);

        $datas = [];

        for ($i = 1; $i < 9; $i++) {
            foreach ($list as $item) {
                $rightsName = $item['标识'];
                if ($rightsName == 'brand_score') {
                    continue;
                }
                if (!isset($this->sourceRightsNameMap[$rightsName])) {
                    echo "$rightsName not found";
                    return false;
                }
                $rightsId = $this->sourceRightsNameMap[$rightsName];
                $rightsType = $this->sourceRightsTypeMap[$rightsId];
                $count = 0;
                if ($rightsType == \Biz\model\brandRights\BrandRightsSource::RIGHTS_TYPE_OPEN) {
                    if (!$item[$i]) {
                        continue;
                    }
                } elseif ($rightsType == \Biz\model\brandRights\BrandRightsSource::RIGHTS_TYPE_LIMIT_NUM) {
                    if ($item[$i] > 0) {
                        $count = $item[$i];
                    } else {
                        continue;
                    }
                } else {
                    if (!$item[$i]) {
                        continue;
                    }
                }
                $datas[$i][] = [
                    'id' => $rightsId,
                    'rights_type' => $rightsType,
                    'rights_name' => $rightsName,
                    'rights_data' => $count,
                ];

            }

        }
        $str = json_encode($datas);

        var_dump($str);

    }

    public function createBrandPackageMigrateData()
    {

        $this->getSourceRightsKv();

        $list = \Biz\model\brandRights\Tmp1::selectList([]);

        $data = [];

        for ($i = 1; $i < 9; $i++) {
            foreach ($list as $item) {
                $rightsName = $item['标识'];
                if (in_array($rightsName,
                    [
                        'blind_box',
                        'goods_test_A',
                        'goods_test_B',
                        'goods_test_C',
                        'new_product',
                        'new_product-goods_test_A',
                        'goods_test_business_cooperation',
                        'brand_score',
                    ])) {
                    //活动权益单独迁移
                    continue;
                }
                if (!isset($this->sourceRightsNameMap[$rightsName])) {
                    echo "$rightsName not found";
                    return false;
                }
                $rightsId = $this->sourceRightsNameMap[$rightsName];
                $rightsType = $this->sourceRightsTypeMap[$rightsId];

                $count = 0;

                if ($rightsType == \Biz\model\brandRights\BrandRightsSource::RIGHTS_TYPE_OPEN) {
                    if (!$item[$i]) {
                        continue;
                    }
                } elseif ($rightsType == \Biz\model\brandRights\BrandRightsSource::RIGHTS_TYPE_LIMIT_NUM) {
                    $count = $item[$i];
                    if ($count == 0) {
                        continue;
                    }
                    $count = $count == -1 ? 99999 : $count;
                } else {
                    if (!$item[$i]) {
                        continue;
                    }
                }

                $data[$i][] = [
                    'rights_source_id' => $rightsId,
                    'rights_name' => $rightsName,
                    'total_num' => $count,
                    'rights_type' => $rightsType,
                ];
            }
        }

        var_dump(json_encode($data));

        return $data;
    }

    private function getBrandContentReceive($brandId)
    {
        $count_where = [
            'brand_id' => $brandId,
            'claim_content_status' => \Biz\model\brand\Content::CLAIM_CONTENT_DONE,
            'audit_status[!]' => \Biz\model\brand\Content::AUDIT_STATUS_AUTO,
        ];
        $count = \Biz\model\brand\Content::count($count_where);
        return $count;
    }

    /**
     * 套餐权益配置 tmp
     * @return array|mixed
     */
    private function packageRightsMap()
    {
        $mapJsonStr = <<<JSON
{"1":[{"id":"37","rights_type":"2","rights_name":"blind_box","rights_data":"2"},{"id":"20","rights_type":"1","rights_name":"brand_buy_link","rights_data":0},{"id":"18","rights_type":"1","rights_name":"brand_content_pv","rights_data":0},{"id":"15","rights_type":"1","rights_name":"brand_conversion_funnel","rights_data":0},{"id":"45","rights_type":"3","rights_name":"brand_ctl_weight","rights_data":0},{"id":"16","rights_type":"1","rights_name":"brand_ctr","rights_data":0},{"id":"17","rights_type":"1","rights_name":"brand_ctr_analysis","rights_data":0},{"id":"35","rights_type":"1","rights_name":"brand_index","rights_data":0},{"id":"13","rights_type":"1","rights_name":"brand_influence","rights_data":0},{"id":"12","rights_type":"1","rights_name":"brand_tag_express","rights_data":0},{"id":"14","rights_type":"1","rights_name":"brand_user_data","rights_data":0},{"id":"19","rights_type":"1","rights_name":"brand_wiki_pv","rights_data":0},{"id":"36","rights_type":"1","rights_name":"brand_zone","rights_data":0},{"id":"23","rights_type":"4","rights_name":"content_receive","rights_data":0},{"id":"3","rights_type":"1","rights_name":"cover_img","rights_data":0},{"id":"21","rights_type":"1","rights_name":"material_center","rights_data":0},{"id":"38","rights_type":"2","rights_name":"new_product","rights_data":"1"},{"id":"50","rights_type":"3","rights_name":"operation_instruction_manual","rights_data":0},{"id":"25","rights_type":"1","rights_name":"relation_wiki","rights_data":0},{"id":"22","rights_type":"1","rights_name":"search_keywords","rights_data":0},{"id":"46","rights_type":"3","rights_name":"traffic_support","rights_data":0},{"id":"47","rights_type":"3","rights_name":"traffic_year_pack","rights_data":0},{"id":"27","rights_type":"1","rights_name":"wiki_detail_sus","rights_data":0},{"id":"11","rights_type":"1","rights_name":"wiki_label","rights_data":0}],"2":[{"id":"4","rights_type":"1","rights_name":"banner","rights_data":0},{"id":"37","rights_type":"2","rights_name":"blind_box","rights_data":"4"},{"id":"1","rights_type":"1","rights_name":"brand_alias","rights_data":0},{"id":"20","rights_type":"1","rights_name":"brand_buy_link","rights_data":0},{"id":"18","rights_type":"1","rights_name":"brand_content_pv","rights_data":0},{"id":"15","rights_type":"1","rights_name":"brand_conversion_funnel","rights_data":0},{"id":"45","rights_type":"3","rights_name":"brand_ctl_weight","rights_data":0},{"id":"16","rights_type":"1","rights_name":"brand_ctr","rights_data":0},{"id":"17","rights_type":"1","rights_name":"brand_ctr_analysis","rights_data":0},{"id":"35","rights_type":"1","rights_name":"brand_index","rights_data":0},{"id":"13","rights_type":"1","rights_name":"brand_influence","rights_data":0},{"id":"12","rights_type":"1","rights_name":"brand_tag_express","rights_data":0},{"id":"14","rights_type":"1","rights_name":"brand_user_data","rights_data":0},{"id":"19","rights_type":"1","rights_name":"brand_wiki_pv","rights_data":0},{"id":"36","rights_type":"1","rights_name":"brand_zone","rights_data":0},{"id":"5","rights_type":"1","rights_name":"card_A","rights_data":0},{"id":"23","rights_type":"4","rights_name":"content_receive","rights_data":0},{"id":"3","rights_type":"1","rights_name":"cover_img","rights_data":0},{"id":"9","rights_type":"1","rights_name":"custom_classify","rights_data":0},{"id":"26","rights_type":"2","rights_name":"forward","rights_data":"1"},{"id":"39","rights_type":"2","rights_name":"goods_test_A","rights_data":"1"},{"id":"44","rights_type":"2","rights_name":"hit","rights_data":"3"},{"id":"2","rights_type":"1","rights_name":"main_shop","rights_data":0},{"id":"21","rights_type":"1","rights_name":"material_center","rights_data":0},{"id":"38","rights_type":"2","rights_name":"new_product","rights_data":"2"},{"id":"50","rights_type":"3","rights_name":"operation_instruction_manual","rights_data":0},{"id":"25","rights_type":"1","rights_name":"relation_wiki","rights_data":0},{"id":"33","rights_type":"1","rights_name":"search_brand","rights_data":0},{"id":"22","rights_type":"1","rights_name":"search_keywords","rights_data":0},{"id":"51","rights_type":"3","rights_name":"service_1v1","rights_data":0},{"id":"46","rights_type":"3","rights_name":"traffic_support","rights_data":0},{"id":"47","rights_type":"3","rights_name":"traffic_year_pack","rights_data":0},{"id":"27","rights_type":"1","rights_name":"wiki_detail_sus","rights_data":0},{"id":"11","rights_type":"1","rights_name":"wiki_label","rights_data":0}],"3":[{"id":"4","rights_type":"1","rights_name":"banner","rights_data":0},{"id":"37","rights_type":"2","rights_name":"blind_box","rights_data":"6"},{"id":"1","rights_type":"1","rights_name":"brand_alias","rights_data":0},{"id":"20","rights_type":"1","rights_name":"brand_buy_link","rights_data":0},{"id":"18","rights_type":"1","rights_name":"brand_content_pv","rights_data":0},{"id":"15","rights_type":"1","rights_name":"brand_conversion_funnel","rights_data":0},{"id":"45","rights_type":"3","rights_name":"brand_ctl_weight","rights_data":0},{"id":"16","rights_type":"1","rights_name":"brand_ctr","rights_data":0},{"id":"17","rights_type":"1","rights_name":"brand_ctr_analysis","rights_data":0},{"id":"35","rights_type":"1","rights_name":"brand_index","rights_data":0},{"id":"13","rights_type":"1","rights_name":"brand_influence","rights_data":0},{"id":"12","rights_type":"1","rights_name":"brand_tag_express","rights_data":0},{"id":"14","rights_type":"1","rights_name":"brand_user_data","rights_data":0},{"id":"19","rights_type":"1","rights_name":"brand_wiki_pv","rights_data":0},{"id":"36","rights_type":"1","rights_name":"brand_zone","rights_data":0},{"id":"5","rights_type":"1","rights_name":"card_A","rights_data":0},{"id":"6","rights_type":"1","rights_name":"card_B","rights_data":0},{"id":"7","rights_type":"1","rights_name":"card_C","rights_data":0},{"id":"23","rights_type":"4","rights_name":"content_receive","rights_data":0},{"id":"3","rights_type":"1","rights_name":"cover_img","rights_data":0},{"id":"9","rights_type":"1","rights_name":"custom_classify","rights_data":0},{"id":"26","rights_type":"2","rights_name":"forward","rights_data":"1"},{"id":"40","rights_type":"2","rights_name":"goods_test_B","rights_data":"1"},{"id":"44","rights_type":"2","rights_name":"hit","rights_data":"5"},{"id":"2","rights_type":"1","rights_name":"main_shop","rights_data":0},{"id":"21","rights_type":"1","rights_name":"material_center","rights_data":0},{"id":"38","rights_type":"2","rights_name":"new_product","rights_data":"4"},{"id":"50","rights_type":"3","rights_name":"operation_instruction_manual","rights_data":0},{"id":"52","rights_type":"3","rights_name":"quarterly_resumption_operation","rights_data":0},{"id":"32","rights_type":"1","rights_name":"recom_daily","rights_data":0},{"id":"28","rights_type":"1","rights_name":"recom_hpage","rights_data":0},{"id":"30","rights_type":"1","rights_name":"recom_nice","rights_data":0},{"id":"29","rights_type":"1","rights_name":"recom_note","rights_data":0},{"id":"31","rights_type":"1","rights_name":"recom_search","rights_data":0},{"id":"25","rights_type":"1","rights_name":"relation_wiki","rights_data":0},{"id":"33","rights_type":"1","rights_name":"search_brand","rights_data":0},{"id":"34","rights_type":"1","rights_name":"search_brand_sug","rights_data":0},{"id":"22","rights_type":"1","rights_name":"search_keywords","rights_data":0},{"id":"51","rights_type":"3","rights_name":"service_1v1","rights_data":0},{"id":"48","rights_type":"3","rights_name":"set_ctr_content","rights_data":0},{"id":"10","rights_type":"1","rights_name":"store","rights_data":0},{"id":"24","rights_type":"1","rights_name":"top","rights_data":0},{"id":"46","rights_type":"3","rights_name":"traffic_support","rights_data":0},{"id":"47","rights_type":"3","rights_name":"traffic_year_pack","rights_data":0},{"id":"8","rights_type":"1","rights_name":"video_card","rights_data":0},{"id":"49","rights_type":"3","rights_name":"wechat_promotion","rights_data":0},{"id":"27","rights_type":"1","rights_name":"wiki_detail_sus","rights_data":0},{"id":"11","rights_type":"1","rights_name":"wiki_label","rights_data":0}],"4":[{"id":"4","rights_type":"1","rights_name":"banner","rights_data":0},{"id":"1","rights_type":"1","rights_name":"brand_alias","rights_data":0},{"id":"20","rights_type":"1","rights_name":"brand_buy_link","rights_data":0},{"id":"18","rights_type":"1","rights_name":"brand_content_pv","rights_data":0},{"id":"15","rights_type":"1","rights_name":"brand_conversion_funnel","rights_data":0},{"id":"45","rights_type":"3","rights_name":"brand_ctl_weight","rights_data":0},{"id":"16","rights_type":"1","rights_name":"brand_ctr","rights_data":0},{"id":"17","rights_type":"1","rights_name":"brand_ctr_analysis","rights_data":0},{"id":"35","rights_type":"1","rights_name":"brand_index","rights_data":0},{"id":"13","rights_type":"1","rights_name":"brand_influence","rights_data":0},{"id":"12","rights_type":"1","rights_name":"brand_tag_express","rights_data":0},{"id":"14","rights_type":"1","rights_name":"brand_user_data","rights_data":0},{"id":"19","rights_type":"1","rights_name":"brand_wiki_pv","rights_data":0},{"id":"36","rights_type":"1","rights_name":"brand_zone","rights_data":0},{"id":"5","rights_type":"1","rights_name":"card_A","rights_data":0},{"id":"6","rights_type":"1","rights_name":"card_B","rights_data":0},{"id":"7","rights_type":"1","rights_name":"card_C","rights_data":0},{"id":"23","rights_type":"4","rights_name":"content_receive","rights_data":0},{"id":"3","rights_type":"1","rights_name":"cover_img","rights_data":0},{"id":"9","rights_type":"1","rights_name":"custom_classify","rights_data":0},{"id":"26","rights_type":"2","rights_name":"forward","rights_data":"1"},{"id":"2","rights_type":"1","rights_name":"main_shop","rights_data":0},{"id":"21","rights_type":"1","rights_name":"material_center","rights_data":0},{"id":"50","rights_type":"3","rights_name":"operation_instruction_manual","rights_data":0},{"id":"25","rights_type":"1","rights_name":"relation_wiki","rights_data":0},{"id":"33","rights_type":"1","rights_name":"search_brand","rights_data":0},{"id":"22","rights_type":"1","rights_name":"search_keywords","rights_data":0},{"id":"46","rights_type":"3","rights_name":"traffic_support","rights_data":0},{"id":"47","rights_type":"3","rights_name":"traffic_year_pack","rights_data":0},{"id":"27","rights_type":"1","rights_name":"wiki_detail_sus","rights_data":0},{"id":"11","rights_type":"1","rights_name":"wiki_label","rights_data":0}],"5":[{"id":"4","rights_type":"1","rights_name":"banner","rights_data":0},{"id":"1","rights_type":"1","rights_name":"brand_alias","rights_data":0},{"id":"20","rights_type":"1","rights_name":"brand_buy_link","rights_data":0},{"id":"18","rights_type":"1","rights_name":"brand_content_pv","rights_data":0},{"id":"15","rights_type":"1","rights_name":"brand_conversion_funnel","rights_data":0},{"id":"45","rights_type":"3","rights_name":"brand_ctl_weight","rights_data":0},{"id":"16","rights_type":"1","rights_name":"brand_ctr","rights_data":0},{"id":"17","rights_type":"1","rights_name":"brand_ctr_analysis","rights_data":0},{"id":"35","rights_type":"1","rights_name":"brand_index","rights_data":0},{"id":"13","rights_type":"1","rights_name":"brand_influence","rights_data":0},{"id":"12","rights_type":"1","rights_name":"brand_tag_express","rights_data":0},{"id":"14","rights_type":"1","rights_name":"brand_user_data","rights_data":0},{"id":"19","rights_type":"1","rights_name":"brand_wiki_pv","rights_data":0},{"id":"36","rights_type":"1","rights_name":"brand_zone","rights_data":0},{"id":"5","rights_type":"1","rights_name":"card_A","rights_data":0},{"id":"6","rights_type":"1","rights_name":"card_B","rights_data":0},{"id":"7","rights_type":"1","rights_name":"card_C","rights_data":0},{"id":"23","rights_type":"4","rights_name":"content_receive","rights_data":0},{"id":"3","rights_type":"1","rights_name":"cover_img","rights_data":0},{"id":"9","rights_type":"1","rights_name":"custom_classify","rights_data":0},{"id":"26","rights_type":"2","rights_name":"forward","rights_data":"1"},{"id":"2","rights_type":"1","rights_name":"main_shop","rights_data":0},{"id":"21","rights_type":"1","rights_name":"material_center","rights_data":0},{"id":"50","rights_type":"3","rights_name":"operation_instruction_manual","rights_data":0},{"id":"32","rights_type":"1","rights_name":"recom_daily","rights_data":0},{"id":"28","rights_type":"1","rights_name":"recom_hpage","rights_data":0},{"id":"30","rights_type":"1","rights_name":"recom_nice","rights_data":0},{"id":"29","rights_type":"1","rights_name":"recom_note","rights_data":0},{"id":"31","rights_type":"1","rights_name":"recom_search","rights_data":0},{"id":"25","rights_type":"1","rights_name":"relation_wiki","rights_data":0},{"id":"33","rights_type":"1","rights_name":"search_brand","rights_data":0},{"id":"34","rights_type":"1","rights_name":"search_brand_sug","rights_data":0},{"id":"22","rights_type":"1","rights_name":"search_keywords","rights_data":0},{"id":"10","rights_type":"1","rights_name":"store","rights_data":0},{"id":"24","rights_type":"1","rights_name":"top","rights_data":0},{"id":"46","rights_type":"3","rights_name":"traffic_support","rights_data":0},{"id":"47","rights_type":"3","rights_name":"traffic_year_pack","rights_data":0},{"id":"8","rights_type":"1","rights_name":"video_card","rights_data":0},{"id":"27","rights_type":"1","rights_name":"wiki_detail_sus","rights_data":0},{"id":"11","rights_type":"1","rights_name":"wiki_label","rights_data":0}],"6":[{"id":"4","rights_type":"1","rights_name":"banner","rights_data":0},{"id":"37","rights_type":"2","rights_name":"blind_box","rights_data":"4"},{"id":"1","rights_type":"1","rights_name":"brand_alias","rights_data":0},{"id":"20","rights_type":"1","rights_name":"brand_buy_link","rights_data":0},{"id":"18","rights_type":"1","rights_name":"brand_content_pv","rights_data":0},{"id":"15","rights_type":"1","rights_name":"brand_conversion_funnel","rights_data":0},{"id":"45","rights_type":"3","rights_name":"brand_ctl_weight","rights_data":0},{"id":"16","rights_type":"1","rights_name":"brand_ctr","rights_data":0},{"id":"17","rights_type":"1","rights_name":"brand_ctr_analysis","rights_data":0},{"id":"35","rights_type":"1","rights_name":"brand_index","rights_data":0},{"id":"13","rights_type":"1","rights_name":"brand_influence","rights_data":0},{"id":"12","rights_type":"1","rights_name":"brand_tag_express","rights_data":0},{"id":"14","rights_type":"1","rights_name":"brand_user_data","rights_data":0},{"id":"19","rights_type":"1","rights_name":"brand_wiki_pv","rights_data":0},{"id":"36","rights_type":"1","rights_name":"brand_zone","rights_data":0},{"id":"5","rights_type":"1","rights_name":"card_A","rights_data":0},{"id":"6","rights_type":"1","rights_name":"card_B","rights_data":0},{"id":"7","rights_type":"1","rights_name":"card_C","rights_data":0},{"id":"23","rights_type":"4","rights_name":"content_receive","rights_data":0},{"id":"3","rights_type":"1","rights_name":"cover_img","rights_data":0},{"id":"9","rights_type":"1","rights_name":"custom_classify","rights_data":0},{"id":"26","rights_type":"2","rights_name":"forward","rights_data":"1"},{"id":"44","rights_type":"2","rights_name":"hit","rights_data":"1"},{"id":"2","rights_type":"1","rights_name":"main_shop","rights_data":0},{"id":"21","rights_type":"1","rights_name":"material_center","rights_data":0},{"id":"42","rights_type":"2","rights_name":"new_product-goods_test_A","rights_data":"3"},{"id":"50","rights_type":"3","rights_name":"operation_instruction_manual","rights_data":0},{"id":"25","rights_type":"1","rights_name":"relation_wiki","rights_data":0},{"id":"33","rights_type":"1","rights_name":"search_brand","rights_data":0},{"id":"22","rights_type":"1","rights_name":"search_keywords","rights_data":0},{"id":"46","rights_type":"3","rights_name":"traffic_support","rights_data":0},{"id":"47","rights_type":"3","rights_name":"traffic_year_pack","rights_data":0},{"id":"27","rights_type":"1","rights_name":"wiki_detail_sus","rights_data":0},{"id":"11","rights_type":"1","rights_name":"wiki_label","rights_data":0}],"7":[{"id":"4","rights_type":"1","rights_name":"banner","rights_data":0},{"id":"37","rights_type":"2","rights_name":"blind_box","rights_data":"6"},{"id":"1","rights_type":"1","rights_name":"brand_alias","rights_data":0},{"id":"20","rights_type":"1","rights_name":"brand_buy_link","rights_data":0},{"id":"18","rights_type":"1","rights_name":"brand_content_pv","rights_data":0},{"id":"15","rights_type":"1","rights_name":"brand_conversion_funnel","rights_data":0},{"id":"45","rights_type":"3","rights_name":"brand_ctl_weight","rights_data":0},{"id":"16","rights_type":"1","rights_name":"brand_ctr","rights_data":0},{"id":"17","rights_type":"1","rights_name":"brand_ctr_analysis","rights_data":0},{"id":"35","rights_type":"1","rights_name":"brand_index","rights_data":0},{"id":"13","rights_type":"1","rights_name":"brand_influence","rights_data":0},{"id":"12","rights_type":"1","rights_name":"brand_tag_express","rights_data":0},{"id":"14","rights_type":"1","rights_name":"brand_user_data","rights_data":0},{"id":"19","rights_type":"1","rights_name":"brand_wiki_pv","rights_data":0},{"id":"36","rights_type":"1","rights_name":"brand_zone","rights_data":0},{"id":"5","rights_type":"1","rights_name":"card_A","rights_data":0},{"id":"6","rights_type":"1","rights_name":"card_B","rights_data":0},{"id":"7","rights_type":"1","rights_name":"card_C","rights_data":0},{"id":"23","rights_type":"4","rights_name":"content_receive","rights_data":0},{"id":"3","rights_type":"1","rights_name":"cover_img","rights_data":0},{"id":"9","rights_type":"1","rights_name":"custom_classify","rights_data":0},{"id":"26","rights_type":"2","rights_name":"forward","rights_data":"1"},{"id":"44","rights_type":"2","rights_name":"hit","rights_data":"2"},{"id":"2","rights_type":"1","rights_name":"main_shop","rights_data":0},{"id":"21","rights_type":"1","rights_name":"material_center","rights_data":0},{"id":"42","rights_type":"2","rights_name":"new_product-goods_test_A","rights_data":"4"},{"id":"50","rights_type":"3","rights_name":"operation_instruction_manual","rights_data":0},{"id":"32","rights_type":"1","rights_name":"recom_daily","rights_data":0},{"id":"28","rights_type":"1","rights_name":"recom_hpage","rights_data":0},{"id":"30","rights_type":"1","rights_name":"recom_nice","rights_data":0},{"id":"29","rights_type":"1","rights_name":"recom_note","rights_data":0},{"id":"31","rights_type":"1","rights_name":"recom_search","rights_data":0},{"id":"25","rights_type":"1","rights_name":"relation_wiki","rights_data":0},{"id":"33","rights_type":"1","rights_name":"search_brand","rights_data":0},{"id":"34","rights_type":"1","rights_name":"search_brand_sug","rights_data":0},{"id":"22","rights_type":"1","rights_name":"search_keywords","rights_data":0},{"id":"10","rights_type":"1","rights_name":"store","rights_data":0},{"id":"24","rights_type":"1","rights_name":"top","rights_data":0},{"id":"46","rights_type":"3","rights_name":"traffic_support","rights_data":0},{"id":"47","rights_type":"3","rights_name":"traffic_year_pack","rights_data":0},{"id":"8","rights_type":"1","rights_name":"video_card","rights_data":0},{"id":"27","rights_type":"1","rights_name":"wiki_detail_sus","rights_data":0},{"id":"11","rights_type":"1","rights_name":"wiki_label","rights_data":0}],"8":[{"id":"4","rights_type":"1","rights_name":"banner","rights_data":0},{"id":"37","rights_type":"2","rights_name":"blind_box","rights_data":"4"},{"id":"1","rights_type":"1","rights_name":"brand_alias","rights_data":0},{"id":"20","rights_type":"1","rights_name":"brand_buy_link","rights_data":0},{"id":"18","rights_type":"1","rights_name":"brand_content_pv","rights_data":0},{"id":"15","rights_type":"1","rights_name":"brand_conversion_funnel","rights_data":0},{"id":"45","rights_type":"3","rights_name":"brand_ctl_weight","rights_data":0},{"id":"16","rights_type":"1","rights_name":"brand_ctr","rights_data":0},{"id":"17","rights_type":"1","rights_name":"brand_ctr_analysis","rights_data":0},{"id":"35","rights_type":"1","rights_name":"brand_index","rights_data":0},{"id":"13","rights_type":"1","rights_name":"brand_influence","rights_data":0},{"id":"12","rights_type":"1","rights_name":"brand_tag_express","rights_data":0},{"id":"14","rights_type":"1","rights_name":"brand_user_data","rights_data":0},{"id":"19","rights_type":"1","rights_name":"brand_wiki_pv","rights_data":0},{"id":"36","rights_type":"1","rights_name":"brand_zone","rights_data":0},{"id":"5","rights_type":"1","rights_name":"card_A","rights_data":0},{"id":"6","rights_type":"1","rights_name":"card_B","rights_data":0},{"id":"7","rights_type":"1","rights_name":"card_C","rights_data":0},{"id":"23","rights_type":"4","rights_name":"content_receive","rights_data":0},{"id":"3","rights_type":"1","rights_name":"cover_img","rights_data":0},{"id":"9","rights_type":"1","rights_name":"custom_classify","rights_data":0},{"id":"26","rights_type":"2","rights_name":"forward","rights_data":"1"},{"id":"44","rights_type":"2","rights_name":"hit","rights_data":"1"},{"id":"2","rights_type":"1","rights_name":"main_shop","rights_data":0},{"id":"21","rights_type":"1","rights_name":"material_center","rights_data":0},{"id":"42","rights_type":"2","rights_name":"new_product-goods_test_A","rights_data":"3"},{"id":"50","rights_type":"3","rights_name":"operation_instruction_manual","rights_data":0},{"id":"32","rights_type":"1","rights_name":"recom_daily","rights_data":0},{"id":"28","rights_type":"1","rights_name":"recom_hpage","rights_data":0},{"id":"30","rights_type":"1","rights_name":"recom_nice","rights_data":0},{"id":"29","rights_type":"1","rights_name":"recom_note","rights_data":0},{"id":"31","rights_type":"1","rights_name":"recom_search","rights_data":0},{"id":"25","rights_type":"1","rights_name":"relation_wiki","rights_data":0},{"id":"33","rights_type":"1","rights_name":"search_brand","rights_data":0},{"id":"34","rights_type":"1","rights_name":"search_brand_sug","rights_data":0},{"id":"22","rights_type":"1","rights_name":"search_keywords","rights_data":0},{"id":"10","rights_type":"1","rights_name":"store","rights_data":0},{"id":"24","rights_type":"1","rights_name":"top","rights_data":0},{"id":"46","rights_type":"3","rights_name":"traffic_support","rights_data":0},{"id":"47","rights_type":"3","rights_name":"traffic_year_pack","rights_data":0},{"id":"8","rights_type":"1","rights_name":"video_card","rights_data":0},{"id":"27","rights_type":"1","rights_name":"wiki_detail_sus","rights_data":0},{"id":"11","rights_type":"1","rights_name":"wiki_label","rights_data":0}]}
JSON;

        $map = json_decode($mapJsonStr, true);
        if (empty($map) || !is_array($map)) {
            return [];
        }
        return $map;
    }


    //老品牌套餐权益tmp1
    private function oldPackageRights()
    {
        $jsonStr = <<<JSON
{"1":[{"rights_source_id":"4","rights_name":"banner","total_num":0,"rights_type":"1"},{"rights_source_id":"20","rights_name":"brand_buy_link","total_num":0,"rights_type":"1"},{"rights_source_id":"18","rights_name":"brand_content_pv","total_num":0,"rights_type":"1"},{"rights_source_id":"15","rights_name":"brand_conversion_funnel","total_num":0,"rights_type":"1"},{"rights_source_id":"45","rights_name":"brand_ctl_weight","total_num":0,"rights_type":"3"},{"rights_source_id":"16","rights_name":"brand_ctr","total_num":0,"rights_type":"1"},{"rights_source_id":"17","rights_name":"brand_ctr_analysis","total_num":0,"rights_type":"1"},{"rights_source_id":"35","rights_name":"brand_index","total_num":0,"rights_type":"1"},{"rights_source_id":"13","rights_name":"brand_influence","total_num":0,"rights_type":"1"},{"rights_source_id":"12","rights_name":"brand_tag_express","total_num":0,"rights_type":"1"},{"rights_source_id":"14","rights_name":"brand_user_data","total_num":0,"rights_type":"1"},{"rights_source_id":"19","rights_name":"brand_wiki_pv","total_num":0,"rights_type":"1"},{"rights_source_id":"36","rights_name":"brand_zone","total_num":0,"rights_type":"1"},{"rights_source_id":"23","rights_name":"content_receive","total_num":0,"rights_type":"4"},{"rights_source_id":"3","rights_name":"cover_img","total_num":0,"rights_type":"1"},{"rights_source_id":"21","rights_name":"material_center","total_num":0,"rights_type":"1"},{"rights_source_id":"50","rights_name":"operation_instruction_manual","total_num":0,"rights_type":"3"},{"rights_source_id":"25","rights_name":"relation_wiki","total_num":0,"rights_type":"1"},{"rights_source_id":"22","rights_name":"search_keywords","total_num":0,"rights_type":"1"},{"rights_source_id":"46","rights_name":"traffic_support","total_num":0,"rights_type":"3"},{"rights_source_id":"47","rights_name":"traffic_year_pack","total_num":0,"rights_type":"3"},{"rights_source_id":"27","rights_name":"wiki_detail_sus","total_num":0,"rights_type":"1"},{"rights_source_id":"11","rights_name":"wiki_label","total_num":0,"rights_type":"1"}],"2":[{"rights_source_id":"4","rights_name":"banner","total_num":0,"rights_type":"1"},{"rights_source_id":"1","rights_name":"brand_alias","total_num":0,"rights_type":"1"},{"rights_source_id":"20","rights_name":"brand_buy_link","total_num":0,"rights_type":"1"},{"rights_source_id":"18","rights_name":"brand_content_pv","total_num":0,"rights_type":"1"},{"rights_source_id":"15","rights_name":"brand_conversion_funnel","total_num":0,"rights_type":"1"},{"rights_source_id":"45","rights_name":"brand_ctl_weight","total_num":0,"rights_type":"3"},{"rights_source_id":"16","rights_name":"brand_ctr","total_num":0,"rights_type":"1"},{"rights_source_id":"17","rights_name":"brand_ctr_analysis","total_num":0,"rights_type":"1"},{"rights_source_id":"35","rights_name":"brand_index","total_num":0,"rights_type":"1"},{"rights_source_id":"13","rights_name":"brand_influence","total_num":0,"rights_type":"1"},{"rights_source_id":"12","rights_name":"brand_tag_express","total_num":0,"rights_type":"1"},{"rights_source_id":"14","rights_name":"brand_user_data","total_num":0,"rights_type":"1"},{"rights_source_id":"19","rights_name":"brand_wiki_pv","total_num":0,"rights_type":"1"},{"rights_source_id":"36","rights_name":"brand_zone","total_num":0,"rights_type":"1"},{"rights_source_id":"5","rights_name":"card_A","total_num":0,"rights_type":"1"},{"rights_source_id":"23","rights_name":"content_receive","total_num":0,"rights_type":"4"},{"rights_source_id":"3","rights_name":"cover_img","total_num":0,"rights_type":"1"},{"rights_source_id":"9","rights_name":"custom_classify","total_num":0,"rights_type":"1"},{"rights_source_id":"26","rights_name":"forward","total_num":"1","rights_type":"2"},{"rights_source_id":"44","rights_name":"hit","total_num":"3","rights_type":"2"},{"rights_source_id":"2","rights_name":"main_shop","total_num":0,"rights_type":"1"},{"rights_source_id":"21","rights_name":"material_center","total_num":0,"rights_type":"1"},{"rights_source_id":"50","rights_name":"operation_instruction_manual","total_num":0,"rights_type":"3"},{"rights_source_id":"25","rights_name":"relation_wiki","total_num":0,"rights_type":"1"},{"rights_source_id":"33","rights_name":"search_brand","total_num":0,"rights_type":"1"},{"rights_source_id":"22","rights_name":"search_keywords","total_num":0,"rights_type":"1"},{"rights_source_id":"51","rights_name":"service_1v1","total_num":0,"rights_type":"3"},{"rights_source_id":"46","rights_name":"traffic_support","total_num":0,"rights_type":"3"},{"rights_source_id":"47","rights_name":"traffic_year_pack","total_num":0,"rights_type":"3"},{"rights_source_id":"27","rights_name":"wiki_detail_sus","total_num":0,"rights_type":"1"},{"rights_source_id":"11","rights_name":"wiki_label","total_num":0,"rights_type":"1"}],"3":[{"rights_source_id":"4","rights_name":"banner","total_num":0,"rights_type":"1"},{"rights_source_id":"1","rights_name":"brand_alias","total_num":0,"rights_type":"1"},{"rights_source_id":"20","rights_name":"brand_buy_link","total_num":0,"rights_type":"1"},{"rights_source_id":"18","rights_name":"brand_content_pv","total_num":0,"rights_type":"1"},{"rights_source_id":"15","rights_name":"brand_conversion_funnel","total_num":0,"rights_type":"1"},{"rights_source_id":"45","rights_name":"brand_ctl_weight","total_num":0,"rights_type":"3"},{"rights_source_id":"16","rights_name":"brand_ctr","total_num":0,"rights_type":"1"},{"rights_source_id":"17","rights_name":"brand_ctr_analysis","total_num":0,"rights_type":"1"},{"rights_source_id":"35","rights_name":"brand_index","total_num":0,"rights_type":"1"},{"rights_source_id":"13","rights_name":"brand_influence","total_num":0,"rights_type":"1"},{"rights_source_id":"12","rights_name":"brand_tag_express","total_num":0,"rights_type":"1"},{"rights_source_id":"14","rights_name":"brand_user_data","total_num":0,"rights_type":"1"},{"rights_source_id":"19","rights_name":"brand_wiki_pv","total_num":0,"rights_type":"1"},{"rights_source_id":"36","rights_name":"brand_zone","total_num":0,"rights_type":"1"},{"rights_source_id":"5","rights_name":"card_A","total_num":0,"rights_type":"1"},{"rights_source_id":"6","rights_name":"card_B","total_num":0,"rights_type":"1"},{"rights_source_id":"7","rights_name":"card_C","total_num":0,"rights_type":"1"},{"rights_source_id":"23","rights_name":"content_receive","total_num":0,"rights_type":"4"},{"rights_source_id":"3","rights_name":"cover_img","total_num":0,"rights_type":"1"},{"rights_source_id":"9","rights_name":"custom_classify","total_num":0,"rights_type":"1"},{"rights_source_id":"26","rights_name":"forward","total_num":"1","rights_type":"2"},{"rights_source_id":"44","rights_name":"hit","total_num":"5","rights_type":"2"},{"rights_source_id":"2","rights_name":"main_shop","total_num":0,"rights_type":"1"},{"rights_source_id":"21","rights_name":"material_center","total_num":0,"rights_type":"1"},{"rights_source_id":"50","rights_name":"operation_instruction_manual","total_num":0,"rights_type":"3"},{"rights_source_id":"52","rights_name":"quarterly_resumption_operation","total_num":0,"rights_type":"3"},{"rights_source_id":"32","rights_name":"recom_daily","total_num":0,"rights_type":"1"},{"rights_source_id":"28","rights_name":"recom_hpage","total_num":0,"rights_type":"1"},{"rights_source_id":"30","rights_name":"recom_nice","total_num":0,"rights_type":"1"},{"rights_source_id":"29","rights_name":"recom_note","total_num":0,"rights_type":"1"},{"rights_source_id":"31","rights_name":"recom_search","total_num":0,"rights_type":"1"},{"rights_source_id":"25","rights_name":"relation_wiki","total_num":0,"rights_type":"1"},{"rights_source_id":"33","rights_name":"search_brand","total_num":0,"rights_type":"1"},{"rights_source_id":"34","rights_name":"search_brand_sug","total_num":0,"rights_type":"1"},{"rights_source_id":"22","rights_name":"search_keywords","total_num":0,"rights_type":"1"},{"rights_source_id":"51","rights_name":"service_1v1","total_num":0,"rights_type":"3"},{"rights_source_id":"48","rights_name":"set_ctr_content","total_num":0,"rights_type":"3"},{"rights_source_id":"10","rights_name":"store","total_num":0,"rights_type":"1"},{"rights_source_id":"24","rights_name":"top","total_num":0,"rights_type":"1"},{"rights_source_id":"46","rights_name":"traffic_support","total_num":0,"rights_type":"3"},{"rights_source_id":"47","rights_name":"traffic_year_pack","total_num":0,"rights_type":"3"},{"rights_source_id":"8","rights_name":"video_card","total_num":0,"rights_type":"1"},{"rights_source_id":"49","rights_name":"wechat_promotion","total_num":0,"rights_type":"3"},{"rights_source_id":"27","rights_name":"wiki_detail_sus","total_num":0,"rights_type":"1"},{"rights_source_id":"11","rights_name":"wiki_label","total_num":0,"rights_type":"1"}],"4":[{"rights_source_id":"4","rights_name":"banner","total_num":0,"rights_type":"1"},{"rights_source_id":"1","rights_name":"brand_alias","total_num":0,"rights_type":"1"},{"rights_source_id":"20","rights_name":"brand_buy_link","total_num":0,"rights_type":"1"},{"rights_source_id":"18","rights_name":"brand_content_pv","total_num":0,"rights_type":"1"},{"rights_source_id":"15","rights_name":"brand_conversion_funnel","total_num":0,"rights_type":"1"},{"rights_source_id":"45","rights_name":"brand_ctl_weight","total_num":0,"rights_type":"3"},{"rights_source_id":"16","rights_name":"brand_ctr","total_num":0,"rights_type":"1"},{"rights_source_id":"17","rights_name":"brand_ctr_analysis","total_num":0,"rights_type":"1"},{"rights_source_id":"35","rights_name":"brand_index","total_num":0,"rights_type":"1"},{"rights_source_id":"13","rights_name":"brand_influence","total_num":0,"rights_type":"1"},{"rights_source_id":"12","rights_name":"brand_tag_express","total_num":0,"rights_type":"1"},{"rights_source_id":"14","rights_name":"brand_user_data","total_num":0,"rights_type":"1"},{"rights_source_id":"19","rights_name":"brand_wiki_pv","total_num":0,"rights_type":"1"},{"rights_source_id":"36","rights_name":"brand_zone","total_num":0,"rights_type":"1"},{"rights_source_id":"5","rights_name":"card_A","total_num":0,"rights_type":"1"},{"rights_source_id":"6","rights_name":"card_B","total_num":0,"rights_type":"1"},{"rights_source_id":"7","rights_name":"card_C","total_num":0,"rights_type":"1"},{"rights_source_id":"23","rights_name":"content_receive","total_num":0,"rights_type":"4"},{"rights_source_id":"3","rights_name":"cover_img","total_num":0,"rights_type":"1"},{"rights_source_id":"9","rights_name":"custom_classify","total_num":0,"rights_type":"1"},{"rights_source_id":"26","rights_name":"forward","total_num":"1","rights_type":"2"},{"rights_source_id":"2","rights_name":"main_shop","total_num":0,"rights_type":"1"},{"rights_source_id":"21","rights_name":"material_center","total_num":0,"rights_type":"1"},{"rights_source_id":"50","rights_name":"operation_instruction_manual","total_num":0,"rights_type":"3"},{"rights_source_id":"25","rights_name":"relation_wiki","total_num":0,"rights_type":"1"},{"rights_source_id":"33","rights_name":"search_brand","total_num":0,"rights_type":"1"},{"rights_source_id":"22","rights_name":"search_keywords","total_num":0,"rights_type":"1"},{"rights_source_id":"46","rights_name":"traffic_support","total_num":0,"rights_type":"3"},{"rights_source_id":"47","rights_name":"traffic_year_pack","total_num":0,"rights_type":"3"},{"rights_source_id":"27","rights_name":"wiki_detail_sus","total_num":0,"rights_type":"1"},{"rights_source_id":"11","rights_name":"wiki_label","total_num":0,"rights_type":"1"}],"5":[{"rights_source_id":"4","rights_name":"banner","total_num":0,"rights_type":"1"},{"rights_source_id":"1","rights_name":"brand_alias","total_num":0,"rights_type":"1"},{"rights_source_id":"20","rights_name":"brand_buy_link","total_num":0,"rights_type":"1"},{"rights_source_id":"18","rights_name":"brand_content_pv","total_num":0,"rights_type":"1"},{"rights_source_id":"15","rights_name":"brand_conversion_funnel","total_num":0,"rights_type":"1"},{"rights_source_id":"45","rights_name":"brand_ctl_weight","total_num":0,"rights_type":"3"},{"rights_source_id":"16","rights_name":"brand_ctr","total_num":0,"rights_type":"1"},{"rights_source_id":"17","rights_name":"brand_ctr_analysis","total_num":0,"rights_type":"1"},{"rights_source_id":"35","rights_name":"brand_index","total_num":0,"rights_type":"1"},{"rights_source_id":"13","rights_name":"brand_influence","total_num":0,"rights_type":"1"},{"rights_source_id":"12","rights_name":"brand_tag_express","total_num":0,"rights_type":"1"},{"rights_source_id":"14","rights_name":"brand_user_data","total_num":0,"rights_type":"1"},{"rights_source_id":"19","rights_name":"brand_wiki_pv","total_num":0,"rights_type":"1"},{"rights_source_id":"36","rights_name":"brand_zone","total_num":0,"rights_type":"1"},{"rights_source_id":"5","rights_name":"card_A","total_num":0,"rights_type":"1"},{"rights_source_id":"6","rights_name":"card_B","total_num":0,"rights_type":"1"},{"rights_source_id":"7","rights_name":"card_C","total_num":0,"rights_type":"1"},{"rights_source_id":"23","rights_name":"content_receive","total_num":0,"rights_type":"4"},{"rights_source_id":"3","rights_name":"cover_img","total_num":0,"rights_type":"1"},{"rights_source_id":"9","rights_name":"custom_classify","total_num":0,"rights_type":"1"},{"rights_source_id":"26","rights_name":"forward","total_num":"1","rights_type":"2"},{"rights_source_id":"2","rights_name":"main_shop","total_num":0,"rights_type":"1"},{"rights_source_id":"21","rights_name":"material_center","total_num":0,"rights_type":"1"},{"rights_source_id":"50","rights_name":"operation_instruction_manual","total_num":0,"rights_type":"3"},{"rights_source_id":"32","rights_name":"recom_daily","total_num":0,"rights_type":"1"},{"rights_source_id":"28","rights_name":"recom_hpage","total_num":0,"rights_type":"1"},{"rights_source_id":"30","rights_name":"recom_nice","total_num":0,"rights_type":"1"},{"rights_source_id":"29","rights_name":"recom_note","total_num":0,"rights_type":"1"},{"rights_source_id":"31","rights_name":"recom_search","total_num":0,"rights_type":"1"},{"rights_source_id":"25","rights_name":"relation_wiki","total_num":0,"rights_type":"1"},{"rights_source_id":"33","rights_name":"search_brand","total_num":0,"rights_type":"1"},{"rights_source_id":"34","rights_name":"search_brand_sug","total_num":0,"rights_type":"1"},{"rights_source_id":"22","rights_name":"search_keywords","total_num":0,"rights_type":"1"},{"rights_source_id":"10","rights_name":"store","total_num":0,"rights_type":"1"},{"rights_source_id":"24","rights_name":"top","total_num":0,"rights_type":"1"},{"rights_source_id":"46","rights_name":"traffic_support","total_num":0,"rights_type":"3"},{"rights_source_id":"47","rights_name":"traffic_year_pack","total_num":0,"rights_type":"3"},{"rights_source_id":"8","rights_name":"video_card","total_num":0,"rights_type":"1"},{"rights_source_id":"27","rights_name":"wiki_detail_sus","total_num":0,"rights_type":"1"},{"rights_source_id":"11","rights_name":"wiki_label","total_num":0,"rights_type":"1"}],"6":[{"rights_source_id":"4","rights_name":"banner","total_num":0,"rights_type":"1"},{"rights_source_id":"1","rights_name":"brand_alias","total_num":0,"rights_type":"1"},{"rights_source_id":"20","rights_name":"brand_buy_link","total_num":0,"rights_type":"1"},{"rights_source_id":"18","rights_name":"brand_content_pv","total_num":0,"rights_type":"1"},{"rights_source_id":"15","rights_name":"brand_conversion_funnel","total_num":0,"rights_type":"1"},{"rights_source_id":"45","rights_name":"brand_ctl_weight","total_num":0,"rights_type":"3"},{"rights_source_id":"16","rights_name":"brand_ctr","total_num":0,"rights_type":"1"},{"rights_source_id":"17","rights_name":"brand_ctr_analysis","total_num":0,"rights_type":"1"},{"rights_source_id":"35","rights_name":"brand_index","total_num":0,"rights_type":"1"},{"rights_source_id":"13","rights_name":"brand_influence","total_num":0,"rights_type":"1"},{"rights_source_id":"12","rights_name":"brand_tag_express","total_num":0,"rights_type":"1"},{"rights_source_id":"14","rights_name":"brand_user_data","total_num":0,"rights_type":"1"},{"rights_source_id":"19","rights_name":"brand_wiki_pv","total_num":0,"rights_type":"1"},{"rights_source_id":"36","rights_name":"brand_zone","total_num":0,"rights_type":"1"},{"rights_source_id":"5","rights_name":"card_A","total_num":0,"rights_type":"1"},{"rights_source_id":"6","rights_name":"card_B","total_num":0,"rights_type":"1"},{"rights_source_id":"7","rights_name":"card_C","total_num":0,"rights_type":"1"},{"rights_source_id":"23","rights_name":"content_receive","total_num":0,"rights_type":"4"},{"rights_source_id":"3","rights_name":"cover_img","total_num":0,"rights_type":"1"},{"rights_source_id":"9","rights_name":"custom_classify","total_num":0,"rights_type":"1"},{"rights_source_id":"26","rights_name":"forward","total_num":"1","rights_type":"2"},{"rights_source_id":"44","rights_name":"hit","total_num":"1","rights_type":"2"},{"rights_source_id":"2","rights_name":"main_shop","total_num":0,"rights_type":"1"},{"rights_source_id":"21","rights_name":"material_center","total_num":0,"rights_type":"1"},{"rights_source_id":"50","rights_name":"operation_instruction_manual","total_num":0,"rights_type":"3"},{"rights_source_id":"25","rights_name":"relation_wiki","total_num":0,"rights_type":"1"},{"rights_source_id":"33","rights_name":"search_brand","total_num":0,"rights_type":"1"},{"rights_source_id":"22","rights_name":"search_keywords","total_num":0,"rights_type":"1"},{"rights_source_id":"46","rights_name":"traffic_support","total_num":0,"rights_type":"3"},{"rights_source_id":"47","rights_name":"traffic_year_pack","total_num":0,"rights_type":"3"},{"rights_source_id":"27","rights_name":"wiki_detail_sus","total_num":0,"rights_type":"1"},{"rights_source_id":"11","rights_name":"wiki_label","total_num":0,"rights_type":"1"}],"7":[{"rights_source_id":"4","rights_name":"banner","total_num":0,"rights_type":"1"},{"rights_source_id":"1","rights_name":"brand_alias","total_num":0,"rights_type":"1"},{"rights_source_id":"20","rights_name":"brand_buy_link","total_num":0,"rights_type":"1"},{"rights_source_id":"18","rights_name":"brand_content_pv","total_num":0,"rights_type":"1"},{"rights_source_id":"15","rights_name":"brand_conversion_funnel","total_num":0,"rights_type":"1"},{"rights_source_id":"45","rights_name":"brand_ctl_weight","total_num":0,"rights_type":"3"},{"rights_source_id":"16","rights_name":"brand_ctr","total_num":0,"rights_type":"1"},{"rights_source_id":"17","rights_name":"brand_ctr_analysis","total_num":0,"rights_type":"1"},{"rights_source_id":"35","rights_name":"brand_index","total_num":0,"rights_type":"1"},{"rights_source_id":"13","rights_name":"brand_influence","total_num":0,"rights_type":"1"},{"rights_source_id":"12","rights_name":"brand_tag_express","total_num":0,"rights_type":"1"},{"rights_source_id":"14","rights_name":"brand_user_data","total_num":0,"rights_type":"1"},{"rights_source_id":"19","rights_name":"brand_wiki_pv","total_num":0,"rights_type":"1"},{"rights_source_id":"36","rights_name":"brand_zone","total_num":0,"rights_type":"1"},{"rights_source_id":"5","rights_name":"card_A","total_num":0,"rights_type":"1"},{"rights_source_id":"6","rights_name":"card_B","total_num":0,"rights_type":"1"},{"rights_source_id":"7","rights_name":"card_C","total_num":0,"rights_type":"1"},{"rights_source_id":"23","rights_name":"content_receive","total_num":0,"rights_type":"4"},{"rights_source_id":"3","rights_name":"cover_img","total_num":0,"rights_type":"1"},{"rights_source_id":"9","rights_name":"custom_classify","total_num":0,"rights_type":"1"},{"rights_source_id":"26","rights_name":"forward","total_num":"1","rights_type":"2"},{"rights_source_id":"44","rights_name":"hit","total_num":"2","rights_type":"2"},{"rights_source_id":"2","rights_name":"main_shop","total_num":0,"rights_type":"1"},{"rights_source_id":"21","rights_name":"material_center","total_num":0,"rights_type":"1"},{"rights_source_id":"50","rights_name":"operation_instruction_manual","total_num":0,"rights_type":"3"},{"rights_source_id":"32","rights_name":"recom_daily","total_num":0,"rights_type":"1"},{"rights_source_id":"28","rights_name":"recom_hpage","total_num":0,"rights_type":"1"},{"rights_source_id":"30","rights_name":"recom_nice","total_num":0,"rights_type":"1"},{"rights_source_id":"29","rights_name":"recom_note","total_num":0,"rights_type":"1"},{"rights_source_id":"31","rights_name":"recom_search","total_num":0,"rights_type":"1"},{"rights_source_id":"25","rights_name":"relation_wiki","total_num":0,"rights_type":"1"},{"rights_source_id":"33","rights_name":"search_brand","total_num":0,"rights_type":"1"},{"rights_source_id":"34","rights_name":"search_brand_sug","total_num":0,"rights_type":"1"},{"rights_source_id":"22","rights_name":"search_keywords","total_num":0,"rights_type":"1"},{"rights_source_id":"10","rights_name":"store","total_num":0,"rights_type":"1"},{"rights_source_id":"24","rights_name":"top","total_num":0,"rights_type":"1"},{"rights_source_id":"46","rights_name":"traffic_support","total_num":0,"rights_type":"3"},{"rights_source_id":"47","rights_name":"traffic_year_pack","total_num":0,"rights_type":"3"},{"rights_source_id":"8","rights_name":"video_card","total_num":0,"rights_type":"1"},{"rights_source_id":"27","rights_name":"wiki_detail_sus","total_num":0,"rights_type":"1"},{"rights_source_id":"11","rights_name":"wiki_label","total_num":0,"rights_type":"1"}],"8":[{"rights_source_id":"4","rights_name":"banner","total_num":0,"rights_type":"1"},{"rights_source_id":"1","rights_name":"brand_alias","total_num":0,"rights_type":"1"},{"rights_source_id":"20","rights_name":"brand_buy_link","total_num":0,"rights_type":"1"},{"rights_source_id":"18","rights_name":"brand_content_pv","total_num":0,"rights_type":"1"},{"rights_source_id":"15","rights_name":"brand_conversion_funnel","total_num":0,"rights_type":"1"},{"rights_source_id":"45","rights_name":"brand_ctl_weight","total_num":0,"rights_type":"3"},{"rights_source_id":"16","rights_name":"brand_ctr","total_num":0,"rights_type":"1"},{"rights_source_id":"17","rights_name":"brand_ctr_analysis","total_num":0,"rights_type":"1"},{"rights_source_id":"35","rights_name":"brand_index","total_num":0,"rights_type":"1"},{"rights_source_id":"13","rights_name":"brand_influence","total_num":0,"rights_type":"1"},{"rights_source_id":"12","rights_name":"brand_tag_express","total_num":0,"rights_type":"1"},{"rights_source_id":"14","rights_name":"brand_user_data","total_num":0,"rights_type":"1"},{"rights_source_id":"19","rights_name":"brand_wiki_pv","total_num":0,"rights_type":"1"},{"rights_source_id":"36","rights_name":"brand_zone","total_num":0,"rights_type":"1"},{"rights_source_id":"5","rights_name":"card_A","total_num":0,"rights_type":"1"},{"rights_source_id":"6","rights_name":"card_B","total_num":0,"rights_type":"1"},{"rights_source_id":"7","rights_name":"card_C","total_num":0,"rights_type":"1"},{"rights_source_id":"23","rights_name":"content_receive","total_num":0,"rights_type":"4"},{"rights_source_id":"3","rights_name":"cover_img","total_num":0,"rights_type":"1"},{"rights_source_id":"9","rights_name":"custom_classify","total_num":0,"rights_type":"1"},{"rights_source_id":"26","rights_name":"forward","total_num":"1","rights_type":"2"},{"rights_source_id":"44","rights_name":"hit","total_num":"1","rights_type":"2"},{"rights_source_id":"2","rights_name":"main_shop","total_num":0,"rights_type":"1"},{"rights_source_id":"21","rights_name":"material_center","total_num":0,"rights_type":"1"},{"rights_source_id":"50","rights_name":"operation_instruction_manual","total_num":0,"rights_type":"3"},{"rights_source_id":"32","rights_name":"recom_daily","total_num":0,"rights_type":"1"},{"rights_source_id":"28","rights_name":"recom_hpage","total_num":0,"rights_type":"1"},{"rights_source_id":"30","rights_name":"recom_nice","total_num":0,"rights_type":"1"},{"rights_source_id":"29","rights_name":"recom_note","total_num":0,"rights_type":"1"},{"rights_source_id":"31","rights_name":"recom_search","total_num":0,"rights_type":"1"},{"rights_source_id":"25","rights_name":"relation_wiki","total_num":0,"rights_type":"1"},{"rights_source_id":"33","rights_name":"search_brand","total_num":0,"rights_type":"1"},{"rights_source_id":"34","rights_name":"search_brand_sug","total_num":0,"rights_type":"1"},{"rights_source_id":"22","rights_name":"search_keywords","total_num":0,"rights_type":"1"},{"rights_source_id":"10","rights_name":"store","total_num":0,"rights_type":"1"},{"rights_source_id":"24","rights_name":"top","total_num":0,"rights_type":"1"},{"rights_source_id":"46","rights_name":"traffic_support","total_num":0,"rights_type":"3"},{"rights_source_id":"47","rights_name":"traffic_year_pack","total_num":0,"rights_type":"3"},{"rights_source_id":"8","rights_name":"video_card","total_num":0,"rights_type":"1"},{"rights_source_id":"27","rights_name":"wiki_detail_sus","total_num":0,"rights_type":"1"},{"rights_source_id":"11","rights_name":"wiki_label","total_num":0,"rights_type":"1"}]}
JSON;
        $data = json_decode($jsonStr, true);

        if (empty($data) || !is_array($data)) {
            return [];
        }
        return $data;
    }

    /**
     * 获取全部套餐
     * @return array|bool
     */
    private function getAllPackages()
    {
        $packages = [];
        $pack_where['package_status'] = \Biz\model\brand\BrandPackage::STATUS_NORMAL;
        $pack_where['pack_type[>]'] = 0;
        $mResult = \Biz\model\brand\BrandPackage::select('*', $pack_where);
        if ($mResult && is_array($mResult)) {
            $packages = $mResult;
        }
        return $packages;
    }

    /**
     * 迁移套餐配置
     * @return false|void
     */
    private function migratePackageRightsConfig()
    {
        echo "run migratePackageRightsConfig" . PHP_EOL;

        $packages = $this->getAllPackages();

        if (empty($packages)) {
            echo "package empty" . PHP_EOL;
            return false;
        }

        $packageRightsMap = $this->packageRightsMap();

        $packageRightsData = [];

        foreach ($packages as $package) {
            if (!isset($packageRightsMap[$package['pack_type']])) {
                echo "pack_type {$package['pack_type']} migrate data not found" . PHP_EOL;
                return false;
            }
            $packageRights = $packageRightsMap[$package['pack_type']];
            foreach ($packageRights as $rights) {
                $rightSourceId = $rights['id'];
                $rightData = $rights['rights_data'] ?? 0;
                $item['package_id'] = $package['id'];
                $item['rights_source_id'] = $rightSourceId;
                $item['rights_data'] = $rightData;
                $item['status'] = \Biz\model\brandRights\BrandPackageRightsConfig::STATUS_NORMAL;
                $packageRightsData[] = $item;
            }
        }
        $bResult = false;
        \Biz\model\brandRights\BrandPackageRightsConfig::action(function () use ($packageRightsData, &$bResult) {
            $result = \Biz\model\brandRights\BrandPackageRightsConfig::insert($packageRightsData)->rowCount();
            if (!$result) {
                echo "insert data to brand_package_rights_config fail" . PHP_EOL;
                echo \Biz\model\brandRights\BrandPackageRightsConfig::sqlLogs() . PHP_EOL;
                return false;
            }
            $bResult = true;
            return true;
        });
        return $bResult;
    }

    /**
     * 获取历史套餐配置信息
     * @return array|bool
     */
    private function getPacksInfo()
    {
        $result = \Biz\model\brandPack\BrandPackInfoModel::select('*', ['pack_type[>]' => 0]);
        $packageInfos = [];
        if ($result && is_array($result)) {
            $packageInfos = $result;
        }
        return $packageInfos;
    }

    /**
     * 获取全部品牌
     * @return array|bool
     */
    private function getBrands()
    {
        $where['status'] = \Biz\model\brand\Brand::STATUS_NORMAL;
        $where['pack_type[>]'] = 0;
        $where['uid[>]'] = 0;
        $result = \Biz\model\brand\Brand::select('*', $where);
        $brands = [];
        if ($result && is_array($result)) {
            $brands = $result;
        }
        return $brands;
    }

    /**
     * 获取品牌id的MAP
     * @return array
     */
    private function brandAccountMap()
    {
        $brandAccountWhere['account_status'] = BrandAccountInfo::ACCOUNT_STATUS_USED;
        $brandAccounts = BrandAccountInfo::select('*',
            $brandAccountWhere);
        if (!$brandAccounts || empty($brandAccounts)) {
            return [];
        }
        $brandAccountsKv = array_column($brandAccounts, null, 'brand_id');
        return $brandAccountsKv;
    }


    private function migrateBrandPackageRights()
    {
        echo "run migrateBrandPackageRights" . PHP_EOL;
        $oldPackageRights = $this->oldPackageRights();
        $brands = $this->getBrands();
        if (empty($brands)) {
            return false;
        }
        $brandAccountsKv = $this->brandAccountMap();

        $insertData = [];
        $insertDataAll = [];

        foreach ($brands as $brand) {
            if (!isset($brandAccountsKv[$brand['id']])) {
                echo "brand account: {$brand['id']} not found \n";
                continue;
            }
            $packType = $brand['pack_type'];
            $in_start_time = strtotime($brandAccountsKv[$brand['id']]['in_start_time']);
            $in_end_time = strtotime($brandAccountsKv[$brand['id']]['in_end_time']);
            $now = time();
            if (!($now >= $in_start_time && $now < $in_end_time)) {
                echo "brand:{$brand['id']} pack_type:{$packType} out service ({$brandAccountsKv[$brand['id']]['in_start_time']} - {$brandAccountsKv[$brand['id']]['in_end_time']})" . PHP_EOL;
                continue;
            }
            if (!isset($oldPackageRights[$packType])) {
                echo "old package rights not found \n";
                return false;
            }
            $oldRights = $oldPackageRights[$packType];
            foreach ($oldRights as $rights) {
                $rightName = $rights['rights_name'];
                $rightsSourceId = $rights['rights_source_id'];
                $item = [];
                $item['rights_num'] = $rights['total_num'] ?? 0;
                $item['rights_name'] = $rightName;
                $item['brand_id'] = $brand['id'];
                $item['rights_source_id'] = $rightsSourceId;
                $item['start_time'] = $in_start_time;
                $item['end_time'] = $in_end_time;
                $item['rights_status'] = \Biz\model\brandRights\BrandPackageRights::STATUS_NORMAL;
                $insertData[] = $item;
                $item['total_num'] = $rights['total_num'] ?? 0;
                if ($rightName == 'content_receive') {
                    $contentRecievedCount = $this->getBrandContentReceive($brand['id']);
                    $contentRecievedCount = $contentRecievedCount ? $contentRecievedCount : 0;
                    $item['rights_num'] = $item['total_num'] - $contentRecievedCount;
                }
                $insertDataAll[] = $item;
            }

        }

        $bResult = false;

        \Biz\model\brandRights\BrandPackageRights::action(function () use ($insertData, $insertDataAll, &$bResult) {
            $result = \Biz\model\brandRights\BrandPackageRights::insert($insertData)->rowCount();
            if (!$result) {
                return false;
            }
            $result = \Biz\model\brandRights\BrandRightsAll::insert($insertDataAll)->rowCount();
            if (!$result) {
                return false;
            }
            $bResult = true;
            return true;
        });

        echo \Biz\model\brandRights\BrandPackageRights::sqlLogs() . PHP_EOL;

        echo \Biz\model\brandRights\BrandRightsAll::sqlLogs() . PHP_EOL;

        return $bResult;

    }


    /**
     * 迁移品牌套餐权益
     * @return false|void
     */
    private function migrateBrandPackageRightsOld()
    {
        echo "run migratePackageRights" . PHP_EOL;

        $packagesInfo = $this->getPacksInfo();

        if (empty($packagesInfo)) {
            return false;
        }

        $packInfoMap = array_column($packagesInfo, null, 'pack_type');

        $brands = $this->getBrands();

        if (empty($brands)) {
            return false;
        }

        $brandAccountsKv = $this->brandAccountMap();

        $insertData = [];
        $insertDataAll = [];

        foreach ($brands as $brand) {
            if (!isset($brandAccountsKv[$brand['id']])) {
                echo "brand account: {$brand['id']} not found \n";
                continue;
            }
            $packType = $brand['pack_type'];
            if (!isset($packInfoMap[$packType])) {
                echo "packinfo packType: {$packType} not found \n";
                return false;
            }

            $in_start_time = strtotime($brandAccountsKv[$brand['id']]['in_start_time']);
            $in_end_time = strtotime($brandAccountsKv[$brand['id']]['in_end_time']);
            $now = time();

            if (!($now >= $in_start_time && $now < $in_end_time)) {
                echo "brand:{$brand['id']} pack_type:{$packType} out service ({$brandAccountsKv[$brand['id']]['in_start_time']} - {$brandAccountsKv[$brand['id']]['in_end_time']})" . PHP_EOL;
                continue;
            }

            $packageInfo = $packInfoMap[$packType];

            $packageInfo['cover_img'] = 1;

            $packageInfo['relation_wiki'] = 1;

            $fields = array_keys($packageInfo);
            foreach ($fields as $key => $column) {
                if (in_array($column,
                    [
                        'id',
                        'pack_id',
                        'pack_type',
                        'status',
                        'create_time',
                        'update_time',
                        'blind_box_num',
                        'new_product_num',
                        'sort',
                        'is_selected_time',
                        'search_brand_alias',

                    ])
                ) {
                    unset($fields[$key]);
                }
            }
            foreach ($fields as $field) {
                $rightName = rtrim($field, '_num');

                if ($rightName == 'card') {
                    if (empty($packageInfo[$rightName])) {
                        continue;
                    }
                    $enableCards = explode(',', trim($packageInfo[$rightName]));
                    $allCardsMap = [
                        2 => 'card_A',
                        3 => 'card_B',
                        4 => 'card_C',
                    ];
                    foreach ($enableCards as $cardIndex) {
                        $rightName = $allCardsMap[$cardIndex];
                        if (!isset($this->sourceRightsNameMap[$rightName])) {
                            echo "source rights not found : {$rightName}" . PHP_EOL;
                            return false;
                        }
                        $item = [];
                        $rightsSourceId = $this->sourceRightsNameMap[$rightName];
                        $item['rights_num'] = 0;
                        $item['rights_name'] = $rightName;
                        $item['brand_id'] = $brand['id'];
                        $item['rights_source_id'] = $rightsSourceId;
                        $item['start_time'] = $in_start_time;
                        $item['end_time'] = $in_end_time;
                        $item['rights_status'] = \Biz\model\brandRights\BrandPackageRights::STATUS_NORMAL;
                        $insertData[] = $item;
                        $item['total_num'] = 0;
                        $insertDataAll[] = $item;
                    }
                    continue;
                }

                if ($rightName == 'show_cate') {
                    if ($packageInfo[$rightName] == 1 || $packageInfo['show_sort'] == 1) {
                        $item = [];
                        $rightName = 'custom_classify';
                        $rightsSourceId = $this->sourceRightsNameMap[$rightName];
                        $item['rights_num'] = 0;
                        $item['rights_name'] = $rightName;
                        $item['brand_id'] = $brand['id'];
                        $item['rights_source_id'] = $rightsSourceId;
                        $item['start_time'] = $in_start_time;
                        $item['end_time'] = $in_end_time;
                        $item['rights_status'] = \Biz\model\brandRights\BrandPackageRights::STATUS_NORMAL;
                        $insertData[] = $item;
                        $item['total_num'] = 0;
                        $insertDataAll[] = $item;
                    }
                    continue;
                }

                if ($rightName == 'show_sort') {
                    continue;
                }

                if ($rightName == 'label') {
                    $packageInfo['wiki_label'] = $packageInfo[$field];
                    unset($packageInfo[$rightName]);
                    $rightName = 'wiki_label';
                }

                if ($rightName == 'search_brand_banner') {
                    if ($packageInfo[$rightName] == 1 || $packageInfo['search_brand_alias'] > 0) {
                        $rightName = 'search_brand';
                        $rightsSourceId = $this->sourceRightsNameMap[$rightName];
                        $item = [];
                        $item['rights_num'] = 0;
                        $item['rights_name'] = $rightName;
                        $item['brand_id'] = $brand['id'];
                        $item['rights_source_id'] = $rightsSourceId;
                        $item['start_time'] = $in_start_time;
                        $item['end_time'] = $in_end_time;
                        $item['rights_status'] = \Biz\model\brandRights\BrandPackageRights::STATUS_NORMAL;
                        $insertData[] = $item;
                        $item['total_num'] = 0;
                        $insertDataAll[] = $item;
                    }
                    continue;
                }

                if (!isset($this->sourceRightsNameMap[$rightName])) {
                    echo " rights_name $rightName not found " . PHP_EOL;
                    return false;
                }
                $rightsSourceId = $this->sourceRightsNameMap[$rightName];
                $rightType = $this->sourceRightsTypeMap[$rightsSourceId];
                $item = [];
                if ($rightType == \Biz\model\brandRights\BrandRightsSource::RIGHTS_TYPE_LIMIT_NUM) {
                    if (empty($packageInfo[$field])) {
                        continue;
                    }
                    $item['rights_num'] = $packageInfo[$field];
                } elseif ($rightType == \Biz\model\brandRights\BrandRightsSource::RIGHTS_TYPE_OPEN) {
                    if (empty($packageInfo[$field])) {
                        continue;
                    }
                }
                if (!isset($item['rights_num'])) {
                    $item['rights_num'] = 0;
                }
                if ($rightName == 'content_receive' && $item['rights_num'] == -1) {
                    $item['rights_num'] = 99999;
                }
                $item['brand_id'] = $brand['id'];
                $item['rights_name'] = $rightName;
                $item['rights_source_id'] = $rightsSourceId;
                $item['start_time'] = $in_start_time;
                $item['end_time'] = $in_end_time;
                $item['rights_status'] = \Biz\model\brandRights\BrandPackageRights::STATUS_NORMAL;
                $insertData[] = $item;
                $item['total_num'] = $item['rights_num'];
                $insertDataAll[] = $item;

            }
        }

        $bResult = false;

        \Biz\model\brandRights\BrandPackageRights::action(function () use ($insertData, $insertDataAll, &$bResult) {
            $result = \Biz\model\brandRights\BrandPackageRights::insert($insertData)->rowCount();
            if (!$result) {
                return false;
            }
            $result = \Biz\model\brandRights\BrandRightsAll::insert($insertDataAll)->rowCount();
            if (!$result) {
                return false;
            }
            $bResult = true;
            return true;
        });

        echo \Biz\model\brandRights\BrandPackageRights::sqlLogs() . PHP_EOL;

        echo \Biz\model\brandRights\BrandRightsAll::sqlLogs() . PHP_EOL;

        return $bResult;

    }

    /**
     * 迁移品牌活动权益
     * @return array|bool
     */
    private function getBrandRights()
    {
        $brandRights = [];
        $result = \Biz\model\brand\BrandRights::selectList(
            [
                'id[>]' => 0,
                'rights_status' => \Biz\model\brand\BrandRights::STATUS_NORMAL,
            ]
        );
        if ($result && is_array($result)) {
            $brandRights = $result;
        }
        return $brandRights;
    }

    /**
     * 迁移活动权益 brand_rights 到 brand_rights_new
     * @return bool
     */
    private function migrateBrandRights()
    {
        echo "run migrateBrandRights" . PHP_EOL;

        $brandsRights = $this->getBrandRights();

        if (empty($brandsRights)) {
            return false;
        }

        $insertData = [];

        $insertDataAll = [];

        foreach ($brandsRights as $brandRight) {
            $now = time();
            if (!($now >= $brandRight['start_time'] && $now < $brandRight['end_time'])) {
                continue;
            }
            if ($brandRight['rights_status'] != \Biz\model\brand\BrandRights::STATUS_NORMAL) {
                continue;
            }
            $rightSourceId = $this->sourceRightsNameMap[$brandRight['rights_name']];
            $item = [];
            $item['brand_id'] = $brandRight['brand_id'];
            $item['rights_name'] = $brandRight['rights_name'];
            $item['rights_source_id'] = $rightSourceId;
            $item['start_time'] = $brandRight['start_time'];
            $item['end_time'] = $brandRight['end_time'];
            $item['rights_status'] = $brandRight['rights_status'];
            $item['rights_num'] = $brandRight['rights_num'];
            $insertData[] = $item;
            $item['total_num'] = $brandRight['total_num'];
            $insertDataAll[] = $item;
        }

        $bResult = false;

        \Biz\model\brandRights\BrandPackageRights::action(function () use ($insertData, $insertDataAll, &$bResult) {
            $result = \Biz\model\brandRights\BrandPackageRights::insert($insertData)->rowCount();
            if (!$result) {
                return false;
            }

            $result = \Biz\model\brandRights\BrandRightsAll::insert($insertDataAll)->rowCount();
            if (!$result) {
                return false;
            }

            $bResult = true;

            return true;
        });

        echo \Biz\model\brandRights\BrandPackageRights::sqlLogs() . PHP_EOL;

        echo \Biz\model\brandRights\BrandRightsAll::sqlLogs() . PHP_EOL;

        return $bResult;

    }

    /**
     * 更新套餐价格
     * @return bool
     */
    private function modifyPackagePrice()
    {
        echo "modifyPackagePrice \n";
        $packages = $this->getAllPackages();
        $packageIds = array_column($packages, 'id');
        $packagesPrice = \Biz\service\BrandRights\BrandPackageRightsConfigService::getPackagesPrice($packageIds);
        foreach ($packages as $package) {
            if (!isset($packagesPrice[$package['id']])) {
                echo "cal package price fail : {$package['id']}" . PHP_EOL;
                return false;
            }
            $cal_price = $packagesPrice[$package['id']]['amount'];
            $data['cal_price'] = $cal_price;
            $data['price'] = $package['discount_price'];
            $data['meal_time'] = 365;
            $data['type'] = 1;
            $where['id'] = $package['id'];
            $where['cal_price'] = $package['cal_price'];
            \Biz\model\brand\BrandPackage::updateData($data, $where);
        }
        return true;
    }

    /**
     * 迁移 brand_rights_recored 到 brand_rights_recored_new
     * @return false|void
     */
    private function migrateRightsRecord()
    {
        echo "run migrateBrandRights" . PHP_EOL;

        $currentId = 0;

        $where = [
            'id[>]' => $currentId,
            'LIMIT' => 1000,
            'ORDER' => ['id' => 'ASC'],
        ];

        $result = \Biz\model\brand\BrandRightsRecored::selectList($where);

        while (is_array($result) && !empty($result)) {
            //迁移数据
            if (!$this->migrateRightsRecordData($result)) {
                return false;
            }

            $perpage_last_record = array_pop($result);

            echo "perpage_last_record:" . $perpage_last_record['id'] . PHP_EOL;

            $where['id[>]'] = $perpage_last_record['id'];

            $result = \Biz\model\brand\BrandRightsRecored::selectList($where);
        }

        return true;

    }

    /**
     * brand_rights_recored 数据清洗
     * @param $records
     * @return false|void
     */
    private function migrateRightsRecordData($records)
    {
        $rightsNamesMap = \Biz\model\brand\BrandRightsRecored::RIGHTS_NAMES;

        foreach ($records as $record) {
            if (!isset($rightsNamesMap[$record['rights_id']])) {
                echo "{$record['rights_id']} not found" . PHP_EOL;
                return false;
            }
            $rightName = $rightsNamesMap[$record['rights_id']];
            if (!isset($this->sourceRightsNameMap[$rightName])) {
                echo "source rights {$rightName} not found" . PHP_EOL;
                return false;
            }
            $rightSourceId = $this->sourceRightsNameMap[$rightName];
            $record['rights_source_id'] = $rightSourceId;
            $record['rights_name'] = $rightName;
            unset($record['rights_id']);
            if ($record['recored_type'] == \Biz\model\brand\BrandRightsRecored::RECORED_TYPE_ADMIN) {
                $sourceIdArr = explode('_', $record['source_id']);
                $sourceRightName = $rightsNamesMap[$sourceIdArr[0]];
                $sourceRightId = $this->sourceRightsNameMap[$sourceRightName];
                $source_id = $sourceRightId . '_' . $sourceIdArr[1];
                $record['source_id'] = $source_id;

                $sourceDataArr = json_decode($record['source_data'], true);
                if (isset($sourceDataArr['rights_id'])) {
                    $sourceRightName = $rightsNamesMap[$sourceDataArr['rights_id']];
                    $sourceRightId = $this->sourceRightsNameMap[$sourceRightName];
                    $sourceDataArr['rights_source_id'] = $sourceRightId;
                    unset($sourceDataArr['rights_id']);
                    $record['source_data'] = json_encode($sourceDataArr);
                    $record['rights_name'] = $sourceRightName;
                }
            }
            $result = \Biz\model\brandRights\BrandRightsRecored::insertData($record);
            $result = strval($result);
            echo "record:{$record['id']} to new table id : {$result}" . PHP_EOL;
        }
        return true;
    }

    /**
     * 数据清洗开始
     * @return $this
     */
    public function start()
    {
        echo "migrate start" . PHP_EOL;

        $this->getSourceRightsKv();

        return $this;
    }

    /**
     * 清洗事务处理
     * @return $this
     */
    public function transTodo()
    {
        $result = $this->migratePackageRightsConfig();

        if (!$result) {
            echo "migratePackageRightsConfig fail!" . PHP_EOL;
            exit;
        }

        echo "migratePackageRightsConfig success" . PHP_EOL;

        sleep(1);

        $result = $this->migrateBrandPackageRights();

        if (!$result) {
            echo "migrateBrandPackageRights fail!" . PHP_EOL;
            exit;
        }

        echo "migrateBrandPackageRights success" . PHP_EOL;

        sleep(1);

        $result = $this->migrateBrandRights();

        if (!$result) {
            echo "migrateBrandRights fail!" . PHP_EOL;
            exit;
        }

        echo "migrateBrandRights success" . PHP_EOL;

        sleep(1);

        $result = $this->modifyPackagePrice();

        if (!$result) {
            echo "modifyPackagePrice fail!" . PHP_EOL;
            exit;
        }

        sleep(1);

        $result = $this->migrateRightsRecord();
        if (!$result) {
            echo "migrateRightsRecord fail!" . PHP_EOL;
            exit;
        }

        echo "migrateRightsRecord success" . PHP_EOL;

        return $this;
    }

    /**
     * 数据迁移完成
     * @return bool
     */
    public function finish()
    {
        echo "migrate finish" . PHP_EOL;

        return true;
    }

}


$migrate = new DataMigrate();

//$migrate->createPackageRightsData();

//$migrate->createBrandPackageMigrateData();

$migrate->start()->transTodo()->finish();